/*
 * @Author: error: git config user.name && git config user.email & please set dead value or install git
 * @Date: 2022-06-22 09:59:58
 * @LastEditors: error: git config user.name && git config user.email & please set dead value or install git
 * @LastEditTime: 2022-06-22 10:28:34
 * @FilePath: \macc.admin.vue\src\projectCommon\store\modules\deviceDetail\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { maccApi, requestHttp } from '@utils/index';

const state = {
  switch: {
    currentDevicePortsData: [],
    currentDevicePortsVlanL3Info: [],
  },
  firewall: {
    currentDevicePortsData: [],
    currentDeviceZone: [],
  },
  //网关数据
  gateWay: {
    currentDevicePortsData: [],
    currentDevicePortsDataInfo: [],
  },
  //ap数据
  ap: {
    currentDevicePortsData: [],
  },
};
const mutations = {
  SET_SWITCH_PORTSDATA(state, payload) {
    state.switch.currentDevicePortsData = payload;
  },
  SET_SWITCH_VLANL3INFO(state, payload) {
    state.switch.currentDevicePortsVlanL3Info = payload;
  },
  SET_FIREWALL_PORTSDATA(state, payload) {
    state.firewall.currentDevicePortsData = payload;
  },
  SET_FIREWALL_ZONEDATA(state, payload) {
    state.firewall.currentDeviceZone = payload;
  },
  SET_GATEWAY_PORTSDATA(state, payload) {
    state.gateWay.currentDevicePortsData = payload;
  },
  SET_GATEWAY_PORTSDATAINFO(state, payload) {
    state.gateWay.currentDevicePortsDataInfo = payload;
  },
  SET_AP_PORTSDATAINFO(state, payload) {
    state.ap.currentDevicePortsData = payload;
  },
};

const actions = {
  /**
   * 获取防火墙端口数据
   * @param {*} param0
   * @param {*} sn
   * @returns
   */
  getFireWallPortsData({ commit }, sn) {
    return new Promise((resolve, reject) => {
      const config = {
        api: maccApi.DEVICE_FIREWALL_INTFINFO_GET_API.replace('{sn}', sn),
        method: 'GET',
      };
      requestHttp(config)
        .then((res) => {
          if (res && res.code == 0) {
            if (res.data) {
              commit('SET_FIREWALL_PORTSDATA', res.data);
              resolve();
            } else {
              reject(res);
            }
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  /**
   * 获取防火墙安全域
   * @param {*} param0
   * @param {*} sn
   * @returns
   */
  getFireWallZoneList({ commit }, sn) {
    return new Promise((resolve, reject) => {
      const config = {
        api: maccApi.DEVICE_FIREWALL_ZONE_ALL_API.replace('{sn}', sn),
        method: 'GET',
      };
      requestHttp(config)
        .then((res) => {
          if (res && res.code == 0) {
            if (res.data) {
              commit('SET_FIREWALL_ZONEDATA', res.data);
              resolve();
            } else {
              reject(res);
            }
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  /**
   * 获取交换机端口数据
   * @param {*} param0
   * @param {*} sn
   * @returns
   */
  getSwitchPortsData({ commit }, sn) {
    return new Promise((resolve, reject) => {
      const params = { snList: [sn] };
      const config = {
        api: maccApi.SWITCH_POTRSLISTBYSNLIST_GET_API,
        method: 'POST',
        params: params,
      };
      requestHttp(config)
        .then((res) => {
          if (res && res.code == 0) {
            if (res.portListMap && res.portListMap[sn]) {
              commit('SET_SWITCH_PORTSDATA', res.portListMap[sn]);
              resolve();
            } else {
              reject(res);
            }
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  /**
   * 获取交换机Vlan配置
   * @param {*} param0
   * @param {*} sn
   * @returns
   */
  getSwitchPortsVlanL3Info({ commit }, sn) {
    return new Promise((resolve, reject) => {
      const config = {
        api: maccApi.SWITCH_NETWORKSWITCHPORTVLANLIST_GET_API.replace(
          '{sn}',
          sn
        ),
        method: 'GET',
      };
      requestHttp(config)
        .then((res) => {
          if (res && res.code == 0) {
            if (res.portList) {
              commit('SET_SWITCH_VLANL3INFO', res.portList);
              resolve();
            } else {
              reject(res);
            }
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  /**
   * 获取网关端口数据
   * @param {*} param0
   * @param {*} sn
   * @returns
   */
  getGateWayPortsData({ commit }, sn) {
    return new Promise((resolve, reject) => {
      const config = {
        api: maccApi.GET_DEVICE_PANELPOERTS,
        method: 'POST',
        params: {
          snList: [sn],
        },
      };
      requestHttp(config)
        .then((res) => {
          if (res && res.code == 0) {
            if (res.portMap) {
              commit('SET_GATEWAY_PORTSDATA', res.portMap[sn]);
              resolve();
            } else {
              reject(res);
            }
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  /**
   * 获取网关端口详细数据
   * @param {*} param0
   * @param {*} sn
   * @returns
   */
  getGateWayPortsDataInfo({ commit }, sn) {
    return new Promise((resolve, reject) => {
      const config = {
        api: maccApi.GET_GATWAY_INTF_INFO.replace('{sn}', sn),
        method: 'GET',
      };
      requestHttp(config)
        .then((res) => {
          if (res && res.code == 0) {
            if (res.data) {
              commit('SET_GATEWAY_PORTSDATAINFO', res.data);
              resolve();
            } else {
              reject(res);
            }
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  //获取 ap端口详细数据
  getEapPortsInfo({ commit }, sn) {
    return new Promise((resolve, reject) => {
      const config = {
        api: maccApi.EAP_PORT_INFO_GET_API.replace('{sn}', sn),
        method: 'GET',
      };
      requestHttp(config)
        .then((res) => {
          if (res && res.code === 0) {
            if (res.portList) {
              commit('SET_AP_PORTSDATAINFO', res.portList);

              resolve();
            } else reject(res);
          } else reject(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
};
const getters = {};

export default {
  namespaced: true, //开启命名空间，更好的区分使用stage,防止不同stage模块中的方法重名
  state,
  getters,
  mutations,
  actions,
};
